define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trace_record/index' + location.search,
                    add_url: 'trace_record/add',
                    edit_url: 'trace_record/edit',
                    del_url: 'trace_record/del',
                    multi_url: 'trace_record/multi',
                    table: 'trace_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: true,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        // {field: 'position_id', title: __('Position_id')},
                        {field: 'position_name', title: __('Position_name'),operate:'like'},
                        {field: 'nickname', title: __('Nickname'),operate:'like'},
                        {field: 'sid', title: __('学号/工号/手机号'),operate:'like'},
                        // {field: 'classes_id', title: __('Classes_id')},
                        {field: 'classes_name', title: __('班级/部门'),operate:false},
                        // {field: 'college_id', title: __('College_id')},
                        {field: 'college_name', title: __('学院/单位'),operate:false},
                        {field: 'create_time', title: __('时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        searc: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trace_record/searc' + location.search,
                    add_url: 'trace_record/add',
                    edit_url: 'trace_record/edit',
                    del_url: 'trace_record/del',
                    multi_url: 'trace_record/multi',
                    table: 'trace_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                searchFormTemplate: 'searchformtpl',
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if($("#sid").val()){
                        filter.sid = $("#sid").val();
                        op.sid = "=";
                    }

                    if($("#create_time").val()){
                        filter.create_time = $("#create_time").val();
                        op.create_time = "between";
                    }

                    if($("#exttime").val()){
                        filter.exttime = $("#exttime").val();
                        op.sid = "=";
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        // {field: 'position_id', title: __('Position_id')},
                        {field: 'position_name', title: __('Position_name'),operate:'like'},
                        {field: 'nickname', title: __('Nickname'),operate:'like'},
                        {field: 'sid', title: __('学号/工号/手机号'),operate:'like'},
                        // {field: 'classes_id', title: __('Classes_id')},
                        {field: 'classes_name', title: __('班级/部门'),operate:'like'},
                        // {field: 'college_id', title: __('College_id')},
                        {field: 'college_name', title: __('学院/单位'),operate:'like'},
                        {field: 'create_time', title: __('时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});